﻿#include "mainwindow.h"
MainWindow* MainWindow::temp_mainwindow = nullptr;
// WiFiWidget* MainWindow::wifiwidget_current = nullptr;
void OnWlanConnectCallback(PWLAN_CONNECTION_NOTIFICATION_DATA pData, DWORD dwConnectCode)
{
    if (pData)
    {
        std::wstring cstrSSID = pData->strProfileName;
        switch (dwConnectCode)
        {
        case wlan_notification_acm_connection_start:
            qDebug() << "开始连接\r\n";
            break;
        case wlan_notification_acm_connection_complete:
        {
            if (pData->wlanReasonCode == WLAN_REASON_CODE_SUCCESS)
            {
                //qDebug()<<"连接成功\r\n";
                //emit wifiwidget_current->complete_();
                //wifiwidget_current->stopAnimation();
                //wifiwidget_current->SetStatus(COMPLETE);
                if (wifiwidget_current) {
                    QMetaObject::invokeMethod(wifiwidget_current, "stopAnimation", Qt::DirectConnection);
                    QMetaObject::invokeMethod(wifiwidget_current, "SetStatus", Qt::DirectConnection, Q_ARG(STATUS, COMPLETE));
                    qDebug()<<"连接成功\r\n";
                }
                else{
                    qDebug()<<"wifiwidget_current为空\r\n";
                }
                //QMetaObject::invokeMethod(wifiwidget_current, "stopAnimation", Qt::DirectConnection);
                //QMetaObject::invokeMethod(wifiwidget_current, "SetStatus", Qt::DirectConnection, Q_ARG(int, COMPLETE));
                //qDebug()<<"连接成功\r\n";
                // wifiwidget_current

            }
            else
            {
                WCHAR wcReasonCode[100] = { 0 };
                WlanReasonCodeToString(pData->wlanReasonCode, 100, wcReasonCode, NULL);
                qDebug() << "连接失败，"; //以这个成功失败作为结果上传
                std::wstring cstrReason = wcReasonCode;
            }
        }
        break;
        case wlan_notification_acm_connection_attempt_fail: //尝试连接失败后会，系统还会发送wlan_notification_acm_connection_complete，标注失败原因
            qDebug()<< "尝试连接失败\r\n";
            break;
        case wlan_notification_acm_disconnecting:
            qDebug() << "正在断开\r\n";
            break;
        case wlan_notification_acm_disconnected:
            qDebug() << "已经断开\r\n";
            break;
        default:
            break;
        }

    }
    else
    {
        switch (dwConnectCode)
        {
        case wlan_notification_acm_scan_complete:
            qDebug()<< "扫描完成\r\n";
            break;
        case wlan_notification_acm_scan_fail:
            qDebug()<< "扫描失败\r\n";
            break;
        default:
            break;
        }
    }
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    //设置窗口大小
    this->setFixedSize(500,600);
    //放入QListWidget进行测试
    wifi_list = new QListWidget(this);
    wifi_list->setFixedSize(500,600);

    WiFiManager& wifi_manager = WiFiManager::GetInstance();
    //注册回调函数
    wifi_manager.RigisterConnectNotification(CONNECT_NOTIFICATION_CALLBACK(OnWlanConnectCallback));
    //声明WiFi信息存储列表
    WiFiInfoList wifilist;
    //声明网卡信息存储
    static InterfaceInfo_List interfaceInfo_list;
    //开始扫描网卡
    wifi_manager.GetWlanInterfaces(interfaceInfo_list);
    //获取第一个网卡接口
    GUID guid = interfaceInfo_list[0];
    //开始扫描WiFi,选择第一个网卡
    wifi_manager.ScanWiFi(guid);
    //获取WiFi信息，存储到wifilist
    wifi_manager.GetWiFiList(wifilist,guid);
    setitem(wifilist,guid);


    // WiFiWidget* wifi_ItemWidget = new WiFiWidget(this);
    // wifi_ItemWidget->SetName("测试WiFi1");
    // QListWidgetItem* pItem = new QListWidgetItem();
    // pItem->setSizeHint(QSize(0, 100));
    // wifi_list->addItem(pItem);
    // wifi_list->setItemWidget(pItem,wifi_ItemWidget);

    wifi_list->show();
    connect(wifi_list, SIGNAL(itemPressed(QListWidgetItem *)), this, SLOT(slotItemPressed(QListWidgetItem *)));
    connect(wifi_list, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), \
            this, SLOT(slotCurrentItemChanged(QListWidgetItem *, QListWidgetItem *)));
    //connect(wifiwidget_current, &WifiWidgetCurrent::complete_, this, SLOT(complete_method()));
}

bool MainWindow::setitem(WiFiInfoList &wifi_info_list,GUID guid)
{
    for(auto wifi_info:wifi_info_list)
    {

        //创建QListWidgetItem进行显示
        QListWidgetItem* pItem = new QListWidgetItem();
        pItem->setSizeHint(QSize(0, 100));
        //将pItem加入WiFi_list
        wifi_list->addItem(pItem);

        //创建自定义的wifi_ItemWidget来显示WiFi信息
        WiFiWidget* wifi_ItemWidget = new WiFiWidget(pItem,this);
        wifi_ItemWidget->SetGuid(guid);
        //将WiFiinfo传入自定义ItemWidget进行信息显示
        wifi_ItemWidget->SetWiFIInfo(wifi_info);
        //将wifi_ItemWidget和pItem相关联
        wifi_list->setItemWidget(pItem,wifi_ItemWidget);
    }
    return true;
}

MainWindow::~MainWindow() {


}
//当item按下时候
void MainWindow::slotItemPressed(QListWidgetItem *item)
{
    QWidget *pItemPre = wifi_list->itemWidget(item);
    //WiFiWidget *pListItemCur = (WiFiWidget*)(pItemPre);
    wifiwidget_current =(WiFiWidget*)(pItemPre);
    //pListItemCur->SetStatus(ENTER);
    //此处242是无效的
    wifiwidget_current->SetStatus(ENTER);
    item->setSizeHint(QSize(242, 200));
}

//当item改变的时候
void MainWindow::slotCurrentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (!current)
        return;
    if ( previous ){
        QWidget *pItemPre = wifi_list->itemWidget(previous);
         WiFiWidget *pListItemCur = (WiFiWidget*)(pItemPre);
        pListItemCur->SetStatus(LEAVE);
        previous->setSizeHint(QSize(242, 100));
    }
    // if ( current ){
    //     QWidget *pItemCur = wifi_list->itemWidget(current);

    //     current->setSizeHint(QSize(242, 200));
    // }
}

void MainWindow::complete_method()
{
    qDebug()<<"test";
    // wifiwidget_current->stopAnimation();
    // wifiwidget_current->SetStatus(COMPLETE);
}

